<template>
    <div class="container">
        <h3>添加原料</h3>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
            <div v-if="showForm">
                <el-form-item label="原料类型：">
                    {{ruleForm.materialType}}
                </el-form-item>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="入库时间：">
                            {{ruleForm.inTime}}
                        </el-form-item>
                        <el-form-item label="入库类型：">
                            {{ruleForm.inType}}
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="进厂编号：">
                            {{ruleForm.inBatch}}
                        </el-form-item>
                        <el-form-item label="仓库：">
                            {{ruleForm.warehouse}}
                        </el-form-item>
                    </el-col>
                </el-row>
                <div v-if="ruleForm.inType == '追溯码入库'">
                    <el-form-item label="追溯码：">
                        {{ruleForm.code}}
                    </el-form-item>
                    <el-row class="container box">
                        <el-col :span="12">
                            <h4>追溯码：</h4>
                            <el-divider></el-divider>
                            <dl>
                                <dt>物料信息</dt>
                                <dd>品名：</dd>
                                <dd>规格：</dd>
                                <dd>保质期：</dd>
                                <dd>生产批次：</dd>
                            </dl>
                            <dl>
                                <dt>相关凭证</dt>
                                <dd>产地与其他：</dd>
                                <dd>检验报告：</dd>
                            </dl>
                        </el-col>
                        <el-col :span="11">
                            <p>计数：</p>
                            <div class="record-tip-con">
                                <el-alert title="追溯码：" type="info"></el-alert>
                            </div>
                        </el-col>
                    </el-row>
                </div>
              <el-form-item label="标准类型：" prop="standard" v-if="ruleForm.materialType == '提取饮片'">
               {{ruleForm.ypstandard}}
              </el-form-item>
                <div v-else-if="ruleForm.inType == '采购入库'">
                    <el-form-item label="品名：">
                        {{ruleForm.breedName}}
                    </el-form-item>
                    <el-form-item label="产地：">
                        {{ruleForm.address}}
                    </el-form-item>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="生产日期：">
                                {{ruleForm.scTime}}
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="有效期至：">
                                {{ruleForm.deadLine}}
                            </el-form-item>
                        </el-col>
                    </el-row>
                </div>
                <div v-else>
                    <el-form-item label="追溯码：">
                        {{ruleForm.sourceCode}}
                    </el-form-item>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="批次：">
                                {{ruleForm.batch}}
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="品名：">
                                {{ruleForm.breedName}}
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-form-item label="物料代码：">
                        {{ruleForm.materialCode}}
                    </el-form-item>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="生产日期：">
                                {{ruleForm.scTime}}
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="有效期至：">
                                {{ruleForm.deadLine}}
                            </el-form-item>
                        </el-col>
                    </el-row>
                </div>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="数量：">
                            {{ruleForm.totalNum}}{{ruleForm.units}}
                        </el-form-item>
                        <el-form-item label="厂家生产批号：">
                            {{ruleForm.materialBatch}}
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item>
                            &nbsp;&nbsp;
                        </el-form-item>
                        <el-form-item label="供应商：">
                            {{ruleForm.supplyCompany}}
                        </el-form-item>
                    </el-col>
                </el-row>
                 <el-row>
                     <el-form-item label="质量标准：">
                             {{ruleForm.qualityStandard}}
                        </el-form-item>

                    <!-- <el-form-item label="质量标准：">
                        <textarea name="" id="" cols="70" rows="10" v-model="ruleForm.qualityStandard"></textarea>
                    </el-form-item> -->

                </el-row>
                <el-form-item class="footerFixed">
                    <el-button type="primary" @click="submitForm" :loading="subLoading">确认无误</el-button>
                    <el-button @click="showForm = false">返回修改</el-button>
                </el-form-item>
            </div>
            <div v-else>
                <el-form-item label="原料类型：" prop="materialType">
                    <el-radio-group v-model="ruleForm.materialType" @change="changeMaterType">
                        <el-radio label="中药材" border>中药材</el-radio>
                        <el-radio label="辅料" border>辅料</el-radio>
<!--                        <el-radio label="提取饮片" border>提取饮片</el-radio>-->
                    </el-radio-group>
                </el-form-item>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="入库时间：" prop="inTime">
                            <el-date-picker type="date" placeholder="选择日期" value-format="yyyy-MM-dd" v-model="ruleForm.inTime" style="width: auto;"></el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="进厂编号：" prop="inBatch" :show-message="showMessage">
                            <el-input placeholder="请输入内容" v-model.trim="ruleForm.inBatch" @change="changeIsBatch" style="width: auto">
                                <!-- <template slot="append"><el-button @click="random">自动生成</el-button></template> -->
                            </el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-form-item label="仓库：">
                    <el-select v-model="ruleForm.warehouse" @change="changeWare" value-key="id" filterable placeholder="请选择" style="width: auto;">
                        <el-option
                            v-for="item in warehouseList"
                            :key="item.id"
                            :label="item.name"
                            :value="item"
                        >
                        {{item.name}}
                        </el-option>
                    </el-select>
                    <el-button style="margin-left:10px" @click="cangLink">仓库管理</el-button>
                </el-form-item>
                <el-form-item label="入库类型：" prop="inType" >
                    <el-radio-group v-model="ruleForm.inType" :disabled="typeDisabled">
                        <el-radio label="追溯码入库" border>追溯码入库</el-radio>
                        <el-radio label="采购入库" border>采购入库</el-radio>
                        <el-radio label="批次入库" border>批次入库</el-radio>
                    </el-radio-group>
                </el-form-item>
              <el-form-item label="标准类型：" prop="standard" v-if="ruleForm.materialType == '提取饮片'">
                <el-select v-model="ruleForm.standard" @change="gettech"  filterable placeholder="请选择" style="width: 300px;">
                  <el-option label="国标" value="1"></el-option>
                  <el-option label='省标' value="2"></el-option>
                </el-select>
              </el-form-item>
                <div v-if="ruleForm.inType == '追溯码入库'">
                    <el-form-item label="追溯码：" prop="sourceCode">
                        <el-input placeholder="手动输入或英文输入法下扫码枪扫描输入" v-model.trim="ruleForm.sourceCode" @blur="onBlur" style="width:400px">
                            <template slot="append">
                                <el-upload
                                    class="upload-daoru"
                                    ref="upload"
                                    :action="action"
                                    :on-change="uploadonchange"
                                    accept=".txt"
                                    :show-file-list='false'
                                    :auto-upload="false"
                                    >
                                    <el-button slot="trigger" size="small">批量导入</el-button>
                                </el-upload>
                                <!-- <el-button size="small" type="primary">批量导入</el-button> -->
                            </template>
                        </el-input>
                    </el-form-item>
                    <el-row class="container box">
                        <el-col :span="12" v-model="list">
                            <h4>追溯码：{{ruleForm.sourceCode}}</h4>
                            <el-divider></el-divider>
                            <dl>
                                <dt>物料信息</dt>
                                <dd>品名：{{list.breedName}}</dd>
                                <dd>规格：{{list.spec}}</dd>
                                <dd>保质期：{{list.deadLine}}</dd>
                                <dd>生产批次：{{list.scBatch}}</dd>
                            </dl>
                            <dl>
                                <dt>相关凭证</dt>
                                <dd>产地与其他：{{list.origin}}</dd>
                                <dd>检验报告：{{list.checkReport}}</dd>
                            </dl>
                        </el-col>
                        <el-col :span="11">
                            <p>计数：{{ruleForm.list.length}}</p>
                            <div class="record-tip-con">
                                <el-alert v-for="(item,index) in ruleForm.list" :key="index" :title="item" type="info" @click.native="handelShow(index)" @close="handleClose(index)"></el-alert>
                            </div>
                        </el-col>
                    </el-row>
                </div>
                <div v-else-if="ruleForm.inType == '采购入库'">
                    <el-form-item label="品名：" prop="breedName">
                        <el-select v-model="ruleForm.breedName" :remote-method="remoteMethod" @change="changeBreed" value-key="id" filterable remote placeholder="请选择" style="width: auto;">
                            <el-option
                                v-for="item in breedList"
                                :key="item.id"
                                :label="item.breedName"
                                :value="item"
                            >
                            </el-option>
                        </el-select>

                        <el-button style="margin-left:10px" @click="wuliaoLink">物料管理</el-button>
                    </el-form-item>
					      <el-form-item v-if="materialType == '中药材'" label="产地：" prop="address">
                        <el-select v-model="ruleForm.address"   filterable remote placeholder="请选择" style="width: auto;">
                            <el-option
                                v-for="item in addresslist"
                                :key="item"
                                :label="item"
                                :value="item"
                            >
                            </el-option>
                        </el-select>
					    <!-- <el-input v-model.trim="ruleForm.address" style="width: auto;"></el-input> -->
					      </el-form-item>
                  <el-form-item v-else label="产地：" prop="address11">
                    <el-select v-model="ruleForm.address"   filterable remote placeholder="请选择" style="width: auto;">
                      <el-option
                          v-for="item in addresslist"
                          :key="item"
                          :label="item"
                          :value="item"
                      >
                      </el-option>
                    </el-select>
                    <!-- <el-input v-model.trim="ruleForm.address" style="width: auto;"></el-input> -->
                  </el-form-item>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="生产日期：" prop="scTime">
                                <el-date-picker type="date" placeholder="选择日期" value-format="yyyy-MM-dd" v-model="ruleForm.scTime" @change="changeSctime" style="width: auto;"></el-date-picker>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="有效期至：">
                                <el-date-picker type="date" placeholder="选择日期" value-format="yyyy-MM-dd" v-model="ruleForm.deadLine" @change="changeDeadLine" style="width: auto;"></el-date-picker>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </div>
                <div v-else>
                    <el-form-item label="追溯码：" prop="sourceCode">
                        <el-input v-model.trim="ruleForm.sourceCode" @blur="blurScode" style="width: 290px"></el-input>
                    </el-form-item>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="批次：">
                                {{ruleForm.batch}}
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="品名：">
                                {{ruleForm.breedName}}
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-form-item label="物料代码：" prop="materialCode">
                        <el-input v-model.trim="ruleForm.materialCode" style="width: 200px"></el-input>
                    </el-form-item>
                    <el-row>
                        <el-col :span="12">
                            <el-form-item label="生产日期：">
                                <el-date-picker type="date" placeholder="选择日期" value-format="yyyy-MM-dd" v-model="ruleForm.scTime" @change="changeSctime" style="width: auto;"></el-date-picker>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="有效期至：">
                                <el-date-picker type="date" placeholder="选择日期" value-format="yyyy-MM-dd" v-model="ruleForm.deadLine" @change="changeDeadLine" style="width: auto;"></el-date-picker>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </div>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="数量：" prop="totalNum" required>
                            <el-input v-model.number="ruleForm.totalNum" type="number" :disabled="isDisabled" @change="changeNum" style="width: 200px">
                                <template slot="append">
                                    <el-select v-model="ruleForm.units" value-key="id" :disabled="unitDis" placeholder="单位" style="width:80px !important">
                                        <el-option
                                            v-for="item in unitList"
                                            :key="item.id"
                                            :label="item.name"
                                            :value="item.name"
                                         >{{item.name}}</el-option>
                                    </el-select>
                                </template>
                            </el-input>
                            <el-button style="margin-left:10px" @click="unitLink">单位管理</el-button>
                        </el-form-item>
                        <el-form-item label="厂家生产批号：">
                            <el-input placeholder="请输入内容" v-model.trim="ruleForm.materialBatch" style="width: auto"></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item>&nbsp;&nbsp;</el-form-item>
                        <el-form-item label="供应商：">
                            <el-select v-model="ruleForm.supplyCompany" @change="changeSupplier" value-key="id" filterable placeholder="请选择" style="width: auto;">
                                <el-option
                                    v-for="item in supplierList"
                                    :key="item.id"
                                    :label="item.supplierName"
                                    :value="item"
                                >{{item.supplierName}}</el-option>
                            </el-select>
                            <el-button style="margin-left:10px" @click="gongyingLink">供应商</el-button>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-form-item label="质量标准：">
                        <textarea name="" id="" cols="70" rows="10" v-model="ruleForm.qualityStandard"></textarea>
                    </el-form-item>

                </el-row>
                <el-form-item class="footerFixed">
                    <el-button type="primary" @click="submitRules('ruleForm')">提交</el-button>
                    <el-button @click="backPage">返回</el-button>
                </el-form-item>
            </div>
        </el-form>
    </div>
</template>

<script>
import {pack_findPackBreedName,materialBreed_findAll,manufacture_findByAllMaterType,material_save,unit_findByList,material_findById,material_update,warehouse_findAll,supplier_findByAll,material_existBatch,receive_search} from "@/request/api";
    export default {
        data() {
            // var validatorNum = (rule, value, callback) => {
            // }
            return {
                subLoading:false,
                addresslist:[],
                showForm: false,
                isDisabled: false,
                typeDisabled: false,
                unitDis: false,
                showMessage: true,
                materialType: '',
                list: {
                    breedName: '',
                    spec: '',
                    deadLine: '',
                    scBatch: '',
                    origin: '',
                    checkReport: ''
                },
                breedList: [],
                unitList: [],
                ruleForm: {
                    materialType: '',
                    inTime: '',
                    inBatch: '',
                    inType: '采购入库',
                    warehouseId: '',
                    warehouse: '',
                    sourceCode: '',
                    deadLine: '',
                    deadLineUnits: '',
                    units: '',
                    breedId:'',
                    breedName: '',
                    materialCode: '',
                    list: [],
                    totalNum: 0,
                    supplyCompany: '',
                    supplyCompanyId: '',
                    batch: '',
                    qualityStandard:''
                },
                rules: {
                    materialType: [
                        { required: true, message: '不能为空', trigger: 'change' },
                    ],
                    sourceCode: [
                        { required: true, message: '不能为空', trigger: 'blur', }
                    ],
                    materialCode: [
                        { required: true, message: '不能为空', trigger: 'blur', }
                    ],
                    inTime: [
                        { required: true, message: '不能为空', trigger: 'change' },
                    ],
                    // scTime: [
                    //     { required: true, message: '不能为空', trigger: 'change' },
                    // ],
                    inBatch: [
                        { required: true, message: '不能为空', trigger: 'blur', }
                    ],
                    breedName: [
                        { required: true, message: '不能为空', trigger: 'change' }
                    ],
                    inType: [
                        { required: true, message: '不能为空', trigger: 'change' },
                    ],
                    totalNum: [
                        { required: true, message: '不能为0', trigger: 'change' },
                    ],
                  address: [
                    { required: true, message: '不能为空', trigger: 'change' },
                  ],
                },
                fileList: [],
                warehouseList: [],
                supplierList: [],
                materialTypeStor: '',
                items:'',
                deadLine: '',
                action: '',
            };
        },
        methods: {
          gettech(val){
            if(this.ruleForm.materialType != "提取饮片"){
              manufacture_findByAllMaterType({
                materialType: this.materialType,

              }).then((res)=>{
                this.breedList = res.data
              })
            }else{
              if(val == "1"){
                this.ruleForm.ypstandard = "国标"
              }
              if(val == "2"){
                this.ruleForm.ypstandard = "省标"
              }
              // console.log(val)
              manufacture_findByAllMaterType({
                materialType: this.materialType,
                ypCodeStandard:val
              }).then((res)=>{
                this.breedList = res.data
              })
            }

          },
			clear(){

			},
			remoteMethod(query){
				// console.log(1111)
				pack_findPackBreedName({
				    materialType: this.materialType,
				    upperCase:query
				}).then((res)=>{
				    this.breedList = res.data
				})
			    // manufacture_findByAllMaterType({
			    //     materialType: this.ruleForm.materialType,
			    //     upperCase:query
			    // }).then((res)=>{
          //
			    //     this.breedList = res.data
			    // })
			},
            backPage () {
                this.$router.go(-1);
                // this.$store.commit('setPagNum',this.$route.query.page)
                this.$store.commit('setStorName',this.materialTypeStor)
            },
            init(){
                material_findById(this.id).then((res) =>{
                    this.ruleForm = res.data
                    if(parseFloat(this.ruleForm.totalNum) != parseFloat(this.ruleForm.surplusNum)){
                        this.isDisabled = true
                        this.unitDis = true
                    }
                    this.materialTypeStor = this.ruleForm.materialType
                })
                this.init1();
            },
            init1(){

                // materialBreed_findAll('').then((res)=>{
                //     this.breedList = res.data
                // })
                unit_findByList('').then((res)=>{
                    this.unitList = res.data
                })
                warehouse_findAll('').then((res) =>{
                    this.warehouseList = res.data
                })
                supplier_findByAll('').then((res)=>{
                    this.supplierList = res.data
                })
            },
            // 选择原料类型
            changeMaterType(val){
                if (val == '中药材') {
                    this.materialType = val
                    this.init1()
                } else if (val == '辅料') {
                    this.materialType = val
                    this.init1()
                } else if (val == '提取饮片') {
                    this.materialType = val
                    this.init1()
                }
            },
            // 选择品名
            changeBreed(ele){
				// console.log(ele)
                this.ruleForm.breedId = ele.id
                this.ruleForm.breedName = ele.breedName
				this.ruleForm.upperCase = ele.upperCase
                this.ruleForm.standard = ele.ypCodeStandard

                this.addresslist = ele.prodAddress.split(",")

				// console.log(this.ruleForm)
                // if (ele.specUnits != '') {
                //     this.ruleForm.units = ele.specUnits.substring(0,ele.specUnits.lastIndexOf("/"))
                //     this.ruleForm.spec = ele.spec
                //     this.ruleForm.specUnits = ele.specUnits
                //     this.unitDis = true
                // }
            },
            // 选择仓库
            changeWare(ele){
                this.ruleForm.warehouseId = ele.id
                this.ruleForm.warehouse = ele.name
            },
            // 溯源码入库
            onBlur () {
                // this.ruleForm.sourceCode = this.ruleForm.sourceCode.substring(this.ruleForm.sourceCode.indexOf("=") + 1).trim();
                // if(this.ruleForm.sourceCode){
                //     if ((this.ruleForm.list.toString()).indexOf(this.ruleForm.sourceCode) != -1) {
                //         this.$Message.error('溯源码已添加!');
                //         this.ruleForm.sourceCode = '';
                //         return;
                //     }
                //     this.$http.get('/material/existSourceCode/'+this.ruleForm.sourceCode).then((res) => {
                //         if (res.data == true) {
                //             this.ruleForm.list.push(this.ruleForm.sourceCode);
                //             this.$http.get('/material/findByCode/'+this.ruleForm.sourceCode).then((res) => {
                //                 this.box = res.data;
                //                 // this.formValidate.materialName = this.box.materialName;
                //             })
                //             this.ruleForm.sourceCode = '';
                //         } else {
                //             this.$Notice.error({
                //             title: '溯源码不存在!',
                //             duration: 8,
                //             });
                //             this.ruleForm.sourceCode = '';
                //         }
                //     })
                // }
            },
            // 批次入库-追溯码查询
            blurScode(val){
                receive_search(this.ruleForm.sourceCode).then((res) =>{
                    this.ruleForm.batch = res.data.plant.batch
                    this.ruleForm.breedName = res.data.plant.breedname
                })
            },
            // 选择供应商
            changeSupplier(ele){
                this.ruleForm.supplyCompany = ele.supplierName
                this.ruleForm.supplyCompanyId = ele.id
            },
            // 数量
            changeNum(){
                if (this.ruleForm.totalNum <= 0) {
                    this.$notify.error({
                        title: '提示',
                        message: '数量不能为零或负数！',
                        showClose: false,
                        duration:2000
                    });
                }
            },
            // 生产日期判断
            changeSctime(val) {
                    var scTime = val
                    var str1 = scTime.toString();
                    str1 = str1.replace('/-/g', '/');
                    var oldTime = new Date(str1).getTime();
                    // var time35 = oldTime + 92016000000
                    // this.ruleForm.deadLine = new Date(time35)
                    // this.deadLine = new Date(time35)
                if (this.ruleForm.deadLine != '') {
                    var deadLine = this.ruleForm.deadLine
                    var str2 = deadLine.toString();
                    str2 = str2.replace('/-/g', '/');
                    var newTime = new Date(str2).getTime();

                    if ( newTime <= oldTime) {
                        this.$notify.error({
                            title: '提示',
                            message: '生产日期不能大于有效期！',
                            showClose: false,
                            duration:2000
                        });
                        this.ruleForm.deadLine = ''
                        return;
                    }
                }
            },
            // 有效期判断
            changeDeadLine(val){
                if (this.ruleForm.scTime != undefined) {
                    var scTime = this.ruleForm.scTime
                    var str1 = scTime.toString();
                    str1 = str1.replace('/-/g', '/');
                    var oldTime = new Date(str1).getTime();

                    var deadLine = val
                    var str2 = deadLine.toString();
                    str2 = str2.replace('/-/g', '/');
                    var newTime = new Date(str2).getTime();

                    if ( newTime <= oldTime) {
                        this.$notify.error({
                            title: '提示',
                            message: '有效期不能小于生产日期！',
                            showClose: false,
                            duration:2000
                        });
                        this.ruleForm.deadLine = ''
                        return;
                    }
                    // var time35 = oldTime + 92016000000
                    // if ( newTime <= time35) {
                    //     this.$notify.error({
                    //         title: '提示',
                    //         message: '有效期至少35月！',
                    //         showClose: false,
                    //         duration:2000
                    //     });
                    //     this.ruleForm.deadLine = this.deadLine
                    //     return;
                    // }
                } else{
                    this.$notify.error({
                        title: '提示',
                        message: '生产日期未填！',
                        showClose: false,
                        duration:2000
                    });
                }
            },
            cangLink(){
                var _this = this
                var data = this.$store.getters.permission
                for (let i = 0; i < data.length; i++) {
                    if(data[i].name == "库存管理"){
                        var items = data[i].children
                        setTimeout(function(){
                            for(var k=0;k<items.length;k++){
                                if(items[k].name=="仓库管理"){
                                    _this.items = 1
                                }
                            }
                        })
                    }
                }
                setTimeout(function(){
                    if(_this.items == "1"){
                        _this.$router.push({
                            path:'/inventory/addInventory'
                        })
                        _this.$store.commit('setStorge',_this.ruleForm)
                    }else{
                        _this.$router.push({
                            path:'/403'
                        })
                    }
                },500)
            },
            wuliaoLink(){
                var data = this.$store.getters.permission
                var items = ""
                for (let i = 0; i < data.length; i++) {
                    if(data[i].name == "物料管理"){
                     items = 1
                    }
                }
                if (items == "1"){
                    this.$router.push({
                        path:'/materialsManagement/added'
                    })
                    this.$store.commit('setStorge',this.ruleForm)
                }else{
                    this.$router.push({
                        path:'/403'
                    })
                }
                // var data = this.$store.getters.permission
                // var items = ""
                // for (let i = 0; i < data.length; i++) {
                //     if(data[i].name == "物料管理"){
                //      items = 1
                //     }
                // }
                // if (items == "1"){
                //     this.$router.push({
                //         path:'/materialsManagement/added'
                //     })
                //     this.$store.commit('setStorge',this.ruleForm)
                // }else{
                //     this.$router.push({
                //         path:'/403'
                //     })
                // }
            },
            unitLink(){
                var data = this.$store.getters.permission
                var items = ""
                for (let i = 0; i < data.length; i++) {
                    if(data[i].name == "单位管理"){
                     items = 1
                    }
                }
                if (items == "1"){
                    this.$router.push({
                        path:'/unit',
                        query:{type:'added'}
                    })
                    this.$store.commit('setStorge',this.ruleForm)
                }else{
                    this.$router.push({
                        path:'/403'
                    })
                }
            },
            gongyingLink(){
                var data = this.$store.getters.permission
                var items = ""
                for (let i = 0; i < data.length; i++) {
                    if(data[i].name == "供应商管理"){
                     items = 1
                    }
                }
                if (items == "1"){
                    this.$router.push({
                        path:'/supplierManagement/added'
                    })
                    this.$store.commit('setStorge',this.ruleForm)
                }else{
                    this.$router.push({
                        path:'/403'
                    })
                }
            },
            random() {
                function randomString(len) {
                　　len = len || 32;
                　　var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';/****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
                　　var maxPos = $chars.length;
                　　var pwd = '';
                　　for (var i = 0; i < len; i++) {
                　　　　pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
                　　}
                　　return pwd;
                }
                this.ruleForm.inBatch = (new Date).toLocaleDateString().replace(/\//g,'') + randomString(12 - (new Date).toLocaleDateString().replace(/\//g,'').length);
                this.showMessage = false
            },
            changeIsBatch(val) {
                material_existBatch({inBatch:val,materialType:this.materialType}).then((res) =>{
                    if (res.data == false) {
                        this.$notify.error({
                            title: '提示',
                            message: '批次号重复，请重新输入！',
                            showClose: false,
                            duration:2000
                        });
                        this.ruleForm.inBatch = "";
                    }
                })
            },
            handleClose(index){
                this.ruleForm.list.splice(index,1)
                this.list = '';
            },
            handelShow(index){
                // this.$http.get('/material/findByCode/'+this.listAlert[index]).then((res) => {
                //     this.list = res.data;
                // })
            },
            handleChange(file) {
                if(file.raw.type!='text/plain'){
                    this.$notify.error({
                        title: '提示',
                        message: '文件格式错误！',
                        showClose: false,
                        duration:2000
                    });
                    return;
                }
                // var reader = new FileReader();//新建一个FileReader
                // reader.readAsText(file, "UTF-8");//读取文件
                // var _vm = this;
                // reader.onload = function(evt){ //读取完文件之后会回来这里
                //     var fileString = evt.target.result; // 读取文件内容
                //     var codeArr =fileString.split('\n')
                //     _vm.$http.get('/material/findByCode/'+codeArr[0]).then((res) => {
                //         if (JSON.stringify(res.data) != "{}") {
                //             _vm.box = res.data;
                //             _vm.formValidate.materialName = _vm.box.materialName;
                //         }
                //     })

                //     codeArr.forEach(code => {
                //     // alert(code);
                //     if(code){
                //         _vm.addCode(null,code);
                //         }
                //     });
                // }
                // return false;
            },
            submitRules(formName){
                this.$refs[formName].validate((valid) => {
                    if (valid) {

                        if (this.ruleForm.inType == '追溯码入库') {
                            if(this.ruleForm.totalNum <= 0){
                                this.$notify.error({
                                    title: '提示',
                                    message: '数量不能为零或负数！',
                                    showClose: false,
                                    duration:2000
                                });
                            }else if (this.ruleForm.units == "") {
                                this.$notify.error({
                                    title: '提示',
                                    message: '数量单位必填！',
                                    showClose: false,
                                    duration:2000
                                });
                            } else{
                                this.showForm = true
                            }
                        } else {
                            if(this.ruleForm.deadLine == ""){
                                if(this.ruleForm.breedName == ''){
                                   this.$notify.error({
                                        title: '提示',
                                        message: '品名必填！',
                                        showClose: false,
                                        duration:2000
                                    });
                                } else if(this.ruleForm.totalNum <= 0){
                                    this.$notify.error({
                                        title: '提示',
                                        message: '数量不能为零或负数！',
                                        showClose: false,
                                        duration:2000
                                    });
                                } else if (this.ruleForm.units == "") {
                                    this.$notify.error({
                                        title: '提示',
                                        message: '数量单位必填！',
                                        showClose: false,
                                        duration:2000
                                    });
                                } else{
                                    this.showForm = true
                                }
                            } else {
                                if(this.ruleForm.breedName == ''){
                                   this.$notify.error({
                                        title: '提示',
                                        message: '品名必填！',
                                        showClose: false,
                                        duration:2000
                                    });
                                }
                                // else if(this.ruleForm.deadLineUnits == ""){
                                //     this.$notify.error({
                                //         title: '提示',
                                //         message: '保质期单位必填！',
                                //         showClose: false,
                                //         duration:2000
                                //     });
                                // }
                                else if(this.ruleForm.totalNum <= 0){
                                    this.$notify.error({
                                        title: '提示',
                                        message: '数量不能为零或负数！',
                                        showClose: false,
                                        duration:2000
                                    });
                                } else if (this.ruleForm.units == "") {
                                    this.$notify.error({
                                        title: '提示',
                                        message: '数量单位必填！',
                                        showClose: false,
                                        duration:2000
                                    });
                                } else{
                                    this.showForm = true
                                }
                            }
                        }
                        let productionTime = new Date(this.ruleForm.scTime).getTime()
                        let storageTime = new Date(this.ruleForm.inTime).getTime()
                        if(productionTime > storageTime){
                            this.$notify.error({
                                title: '提示',
                                message: '生产日期不得晚于入库时间',
                                showClose: false,
                                duration:2000
                            });
                            this.showForm = false
                            return
                        }


                    } else {
                        return false;
                    }
                });
                // this.showForm = true
            },
            submitForm() {
                this.subLoading = true
                if(this.id == undefined){
                    if (this.ruleForm.inType == '批次入库') {
                        this.ruleForm.list = []
                    }
                    material_save(this.ruleForm).then((res) => {
                        this.$notify({
                            title: '提示',
                            message: '保存成功',
                            type: 'success',
                            duration:1000
                        });
                        this.subLoading = false
                        this.backPage()
                    })
                } else{
					if(this.ruleForm.surplusNum == this.ruleForm.totalNum){
						this.ruleForm.surplusNum = this.ruleForm.totalNum
					}

                    // console.log(this.ruleForm,2222)
                    material_update(this.ruleForm).then((res) => {
                        this.$notify({
                            title: '提示',
                            message: '修改成功',
                            type: 'success',
                            duration:1000
                        });
                        this.subLoading = false
                        this.backPage()
                    })
                }
            },
            // 追溯码入库
            uploadonchange(file) {
                let formData = new FormData();
                formData.append('file', file.raw);
                let config = {
                    headers:{'Content-Type':'multipart/form-data'}
                };
                console.log(file,11)
                // materialBreed_inmaterial(formData).then((res) =>{
                //     if (res.code == 200) {
                //         this.$alert('已导入'+res.data+'条', '导入成功', {
                //             confirmButtonText: '确定',
                //             callback: action => {
                //                 this.$message({
                //                     type: 'info',
                //                     message: `action: ${ action }`
                //                 });
                //             }
                //         });
                //         // this.$notify({
                //         //     title: '操作成功',
                //         //     message: '导入成功',
                //         //     type: 'success'
                //         // });
                //         this.init()
                //     } else{
                //         this.$notify.error({
                //             title: '操作失败',
                //             message: '导入失败',
                //             type: 'error'
                //         });
                //     }
                // })
            }
        },
        mounted(){
            if(this.$route.query.type == '中药材'){
                this.ruleForm.materialType = '中药材'
                this.materialType = '中药材'
            } else if(this.$route.query.type == '辅料'){
                this.ruleForm.materialType = '辅料'
                this.materialType = '辅料'
            } else if(this.$route.query.type == '提取饮片'){
                this.ruleForm.materialType = '提取饮片'
                this.materialType = '提取饮片'
            }
            var ruleForm = this.$store.state.newStorge
            // console.log(ruleForm,1221)
          this.gettech()
            if (ruleForm != null) {
                if (ruleForm.inType == '采购入库') {
                    this.ruleForm = this.$store.state.newStorge
                } else if (ruleForm.inType == '追溯码入库'){
                    this.ruleForm = this.$store.state.newStorge
                } else if (ruleForm.inType == '批次入库'){
                    this.ruleForm = this.$store.state.newStorge
                }

                if(ruleForm.materialType == '中药材'){
                    this.ruleForm.materialType = '中药材'
                    this.ruleForm = this.$store.state.newStorge
                } else if(ruleForm.materialType == '辅料'){
                    this.ruleForm.materialType = '辅料'
                    this.ruleForm = this.$store.state.newStorge
                } else if(ruleForm.materialType == '提取饮片'){
                    this.ruleForm.materialType = '提取饮片'
                    this.ruleForm = this.$store.state.newStorge
                }
            }
            this.id = this.$route.query.id
            if(this.id == undefined){
                this.init1()
                if(this.$route.query.type == '中药材'){
                    this.materialTypeStor = '中药材'
                } else if(this.$route.query.type == '辅料'){
                    this.materialTypeStor = '辅料'
                } else if(this.$route.query.type == '提取饮片'){
                    this.materialTypeStor = '提取饮片'
                }
                this.$store.commit('removeRuform','')
            }else{
                this.init()
                this.typeDisabled = true
            }
        },
    }
</script>

<style lang="less" scoped>
.el-date-editor.el-input{
    width: 200px !important
}
.el-select{
    width: 200px !important
}
.demo-ruleForm{
    margin-top: 10px;
}
.demo-ruleForm .el-radio--small.is-bordered{
    width: 120px !important;
    margin-right: 10px !important;
}
.el-menu-item.is-active{
    color: #409EFF !important;
    background: #ECF5FF !important;
    span{
        color: #409EFF !important;
    }
}
.box{
    width: 85%;
    margin-bottom: 15px;
    dl{
        margin-bottom: 20px;
        overflow: hidden;
        dt{ margin-bottom: 5px; font-size: 14px;}
        dd{ font-weight: 500; font-size: 12px; float: left; width: 50%; line-height: 24px;}
    }
    dl:last-child{
        margin-bottom: 0;
    }
    .el-col-11{
        height: 200px;
        overflow-y: auto;
        margin-left: 10px;
        border-left: 1px solid #EBEEF5;
        padding-left: 10px;
        p{ color: #303133;}
        .record-tip-con{
            padding-top: 10px;
        }
    }
}
.el-upload--text{
    border:0;
    // width: auto;
    // height: auto;
    background: none;
    display: block;
    .el-button--small{
        padding: 10px 20px;
    }
}
// 深度作用器
/deep/.upload-daoru{
    .el-upload{
        border: none;
        width: auto;
        height: auto;
        border-radius: 0;
        float: right;
        background: none;
    }
}
</style>
